<script lang="ts" setup>
import {
  ElButton,
  ElInput,
  ElMessageBox,
  ElMessage,
  ElRow,
  ElCol,
  ElProgress,
  ElTooltip,
} from "element-plus";
import { Plus } from "@vben/icons";
import { onMounted, ref } from "vue";
import { list } from "#/api/sys/media";
import { useDebounceFn } from "@vueuse/core";
import { router } from "#/router";

const name = ref("");
const tableData = ref([]);

const spaceInput = (value: string) => {
  // 过滤空格
  name.value = value.replace(/\s+/g, "");
};

const search = async () => {
  const params = {
    deviceName: name.value,
  };
  const { data } = await list(params);
  if (data.length > 0) {
    tableData.value = data;
  } else {
    tableData.value = [];
  }
};

const debouncedSearch = useDebounceFn(search, 500);

const detailPage = (item) => {
  router.push({
    name: "MediaDataDetail",
    query: {
      deviceId: item.id,
      deviceType: item.deviceType,
      parentId: 0,
      name: item.deviceName,
    },
  });
};

const resetForm = () => {
  name.value = "";
  search();
};

onMounted(() => {
  search();
});
</script>

<template>
  <div class="p-2 pageStyle">
    <!-- <div class="top">
      <h3 class="top-title">媒体数据</h3>
      <div class="data">
        <div class="left">
          <ElProgress
            type="dashboard"
            :percentage="80"
            :stroke-width="10"
            color="#2F69EB"
            :width="90"
            :height="90"
          >
            <template #default="{ percentage }">
              <div class="percentage-flex">
                <span class="percentage-value">{{ percentage }}%</span>
                <span class="percentage-label">使用率</span>
              </div>
            </template>
          </ElProgress>
        </div>
        <div class="right">
          <div class="space-info">
            <div class="used-space">
              <span class="number">15MB</span>
              <span class="label">已占资源</span>
            </div>
            <div class="total-space">
              <span class="number">200MB</span>
              <span class="label">总资源</span>
            </div>
          </div>
        </div>
      </div>
    </div> -->
    <div>
      <div class="search">
        <ElRow>
          <ElCol :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <div class="searchForm">
              <div>
                <span>设备名称：</span>
                <ElInput
                  v-model="name"
                  style="
                    width: 240px;
                    margin-right: 16px;
                    color: rgba(48, 49, 51, 0.85);
                  "
                  placeholder="请输入"
                  clearable
                  @input="spaceInput"
                />
              </div>
              <div class="searchBtn">
                <ElButton
                  type="primary"
                  style="margin-left: 10px"
                  @click="debouncedSearch"
                  >查询</ElButton
                >
                <ElButton @click="resetForm()">重置</ElButton>
              </div>
            </div>
          </ElCol>
        </ElRow>
      </div>
      <div class="list">
        <!-- 数据为空时显示暂无数据 -->
        <div v-if="tableData.length === 0" class="empty-data">
          <!-- <img src="/static/mediaList/nodata.png" alt="暂无数据" class="empty-image" /> -->
          <p class="empty-text">暂无数据</p>
        </div>
        <!-- 数据列表 -->
        <div v-else v-for="item in tableData" :key="item.id" class="item" @click="detailPage(item)">
          <div class="imgBox">
            <img src="/static/file.png" alt="" />
          </div>
          <div>
            <!-- <ElTooltip :content="item.deviceName" placement="top"> -->
              <h3 class="ellipsis">{{ item.deviceName }}</h3>
            <!-- </ElTooltip> -->
            <p>最新采集数据时间：{{ item.mediaUploadTime }}</p>
            <!-- <p>硬盘使用情况：200G可用，共200G</p> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="css" scoped>
.top {
  width: 280px;
  height: 132px;
  background: #ffffff;
  margin-bottom: 8px;
  padding-top: 12px;
  padding-left: 12px;
}

.top-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 12px;
}

.data {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.left {
  width: 90px;
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.percentage-flex {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.percentage-value {
  font-weight: 900;
  font-size: 20px;
  color: #303133;
  margin-bottom: 4px;
}

.percentage-label {
  font-size: 14px;
  color: #303133;
}

:deep(.el-progress-circle__track) {
  stroke: #cde8ff !important;
}

.right {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 121px;
  height: auto;
  position: relative;
  margin-right: 22px;
}

.space-info {
  display: flex;
  justify-content: space-between;
  align-content: center;
  width: 121px;
  height: auto;
}

.used-space,
.total-space {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.space-info::after {
  content: "";
  height: 100%;
  position: absolute;
  left: 50%;
  width: 1px;
  transform: translateX(-50%);
  background: #f2f3f7;
}

.total-space {
  margin-left: 12px;
}

.number {
  font-size: 14px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 2px;
}

.label {
  font-size: 12px;
  color: #909399;
}

.search {
  padding: 16px 24px 20px 16px;
  background: #fff;
}

.searchForm {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  font-size: 14px;
}

.el-row {
  display: flex;
  place-content: center center;
}

.list {
  background: #fff;
  display: flex;
  flex-wrap: wrap;
  place-content: flex-start flex-start;
  padding-left: 12px;
  /* height: calc(100vh - 310px); */
  height: calc(100vh - 168px);
  overflow: scroll;
}

.item {
  width: 282px;
  height: 282px;
  border-radius: 1px;
  border: 1px solid #ebeef5;
  margin-right: 16px;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  text-align: center;
  cursor: pointer;
}

.imgBox {
  width: 100%;
  height: auto;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

img {
  width: 80px;
  height: 62px;
  margin-top: 46px;
  margin-bottom: 32px;
}

h3 {
  font-weight: 500;
  font-size: 16px;
  color: #303133;
  margin-bottom: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

p {
  font-size: 12px;
  margin-bottom: 8px;
  color: #606266;
}

/* 暂无数据样式 */
.empty-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.empty-image {
  width: 200px;
  height: 150px;
  margin: 0 0 24px 0;
  opacity: 0.5;
}

.empty-text {
  font-size: 14px;
  color: #909399;
  margin: 0;
}
</style>